#include "KeyNumberControl.h"
#define KEYSNCONFIGFILE_PATH ".\\DataIO\\KeySNConfig.xml"
#define	FORMATPAGEDATA_PATH ".\\DataIO\\FormatPageData.xml"
#include <io.h>
#include <TinyXml/tinyxml.h>
#include"EventName.h"

KeyNumberControl::KeyNumberControl()
{	
	m_IsLoad = false;
	m_IsLoad = initialization();//初始化
}
bool KeyNumberControl::initialization()
{
	return loadKeySNConfigFile(KEYSNCONFIGFILE_PATH);
}
/************************************
*@brief 加载连接文件
*@note 读取KeySN配置文件信息
*@param filePath KeySN配置文件路径
*@return 是否加载成功
*************************************/
bool KeyNumberControl::loadKeySNConfigFile(const std::string &filePath)
{
	if (filePath.empty())
	{
		return false;
	}
	m_KeySNConfigFilePath = filePath;
	//判定文件是否存在
	if (_access(m_KeySNConfigFilePath.c_str(), 0) != 0)
	{
		return false;
	}

	//读取Xml文件
	TiXmlDocument ddLoadXml;
	if (!ddLoadXml.LoadFile(m_KeySNConfigFilePath.c_str()))
	{
		return false;
	}
	TiXmlElement* pRootElement = ddLoadXml.FirstChildElement("KeySNConfigs");
	if (pRootElement == nullptr)
	{
		return false;
	}
	for (TiXmlElement* pConnectionElement = pRootElement->FirstChildElement("config"); pConnectionElement; pConnectionElement = pConnectionElement->NextSiblingElement("config"))
	{
		KeyData* keydata = new KeyData;
		int msgid = -1;
		for (TiXmlElement* pPropertyElement = pConnectionElement->FirstChildElement("property"); pPropertyElement; pPropertyElement = pPropertyElement->NextSiblingElement("property"))
		{
			std::string propertyName = pPropertyElement->Attribute("name");

			if (propertyName == "keySN")
			{
				keydata->m_KeySN = atoi(pPropertyElement->Attribute("value"));
			}
			else if (propertyName == "localKeySN")
			{
				keydata->m_LocalKeySN = atoi(pPropertyElement->Attribute("value"));
			}
			else if (propertyName == "formatName")
			{
				keydata->m_FormatName = pPropertyElement->Attribute("value");
			}
			else if (propertyName == "moduleName")
			{
				keydata->m_ModuleName = pPropertyElement->Attribute("value");
			}
			else if (propertyName == "formatPage")
			{
				keydata->m_PageNum = atoi(pPropertyElement->Attribute("value"));
			}
			else if (propertyName == "msgID")
			{
				msgid = atoi(pPropertyElement->Attribute("value"));
			}
		}
		if (0 > keydata->m_KeySN || keydata->m_ModuleName.empty() || 0 > keydata->m_LocalKeySN || 0 > keydata->m_PageNum)
		{
			continue;
		}
		//m_ConnectionInfoMap.insert(std::pair<string, ConnectioInfo>(connectioInfo.appName, connectioInfo));
		//m_KeySNConfigTable.insert(std::pair<std::string, KeyData>(keydata->m_KeySN, keydata));//appName改为connectionName
		//关联keySN相关数据
		m_KeySNConfigTable[keydata->m_KeySN] = keydata;
		//关联msgid和KeySN
		m_MsgIDToKeySNMap[msgid] = keydata->m_KeySN;
		if (m_KeySNConfigTable.size() <= 0 && m_MsgIDToKeySNMap.size() <= 0)
		{
			return false;
		}
		//发送初始化data事件
		//int size = analyzingMsgID(EVENTMSG_RECEIVEDATA_TESTINIT, &keydata->m_Data, NULL);
		/*if (size <= 0)
		{
			return false;
		}*/
	}
	return true;
	//return (m_KeySNConfigTable.size() > 0 && m_MsgIDToKeySNMap.size() > 0) ? true : false;
}

void KeyNumberControl::setMap(std::string formatName, std::map<int, char*>& destMap)
{
	for each (std::pair<int, KeyData*> it in m_KeySNConfigTable)
	{
		if (formatName == it.second->m_FormatName)
		{
			const int page =  it.second->m_PageNum;
			destMap[page] = it.second->m_Data;
		}
	}
}
KeyNumberControl::~KeyNumberControl()
{
	for each(std::pair<int, KeyData*>keySNConfigTablePair in m_KeySNConfigTable)
	{
		if (keySNConfigTablePair.second->m_Data)
		{
			delete keySNConfigTablePair.second->m_Data;
		}
		delete keySNConfigTablePair.second;
	}
	m_KeySNConfigTable.clear();
}

/************************************
*@brief 获取keySN，module按键关联信息
*@param 全局按键编号
*************************************/
KeyData* KeyNumberControl::getKeyNumber(int keySN)
{
	return m_KeySNConfigTable[keySN];
}

/************************************
*@brief 加载KeySNConfigTable文件信息是否成功
*@return 是否加载成功
*************************************/
bool KeyNumberControl::isLoad()
{
	return m_IsLoad;
}

/************************************
*@brief 用过消息ID获取到界面名字等信息
*@note 读取KeySN配置文件信息
*@param filePath KeySN配置文件路径
*@return 是否加载成功
*************************************/
KeyData* KeyNumberControl::getFormatNameByMsgID(int msgID)
{
	return m_KeySNConfigTable[m_MsgIDToKeySNMap[msgID]];
}

/************************************
*@brief 解析消息id
*@note 根据消息id 修改数据 发送数据
*@param msgID 按键编号
*@return -1 0 失败，其他成功
*************************************/
int KeyNumberControl::analyzingMsgID(int msgID, char** destData, char** srcData )
{
	int size = -1;
	
	switch (msgID)
	{
	case EVENTMSG_BOTTON_NUMBER_WRONG:
	{
										 printf("按键编号错误\n");
	}
		break;
	case EVENTMSG_BOTTON_ADD_RADIUS:
	{
									   Key1Data* const realDestData = (Key1Data*)*destData;
									   Key1Data* realSrcData = (Key1Data*)*srcData;
									   realDestData->reduis += 100;
									   size = sizeof(realDestData);
									   printf("按下了11号按钮，圆形变大\n");
	}
		break;
	case EVENTMSG_BOTTON_REDUCE_RADIUS:
	{
										  Key1Data* const realDestData = (Key1Data*)*destData;
										  Key1Data* realSrcData = (Key1Data*)*srcData;
										  realDestData->reduis -= 100;
										  size = sizeof(*srcData);
										  printf("按下了12号按钮，圆形变小\n");
	}
		break;
	case EVENTMSG_RECEIVEDATA_TESTINIT:
	{
										  *destData = (char*)new Key1Data;
										  ((Key1Data*)*destData)->reduis = 0;
										  size = sizeof((Key1Data*)*destData);
										  printf("初始化结构体 Key1Data \n");
	}
		break;
	default:
	{

	}
		break;
	}
	return size;
}




